namespace NuGetGallery.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class RemoveVulnerabilityDataFromPackageDeprecationModel : DbMigration
    {
        public override void Up()
        {
            DropForeignKey("dbo.PackageDeprecationCves", "PackageDeprecation_Key", "dbo.PackageDeprecations");
            DropForeignKey("dbo.PackageDeprecationCves", "Cve_Key", "dbo.Cves");
            DropForeignKey("dbo.PackageDeprecationCwes", "PackageDeprecation_Key", "dbo.PackageDeprecations");
            DropForeignKey("dbo.PackageDeprecationCwes", "Cwe_Key", "dbo.Cwes");
            DropIndex("dbo.Cves", new[] { "CveId" });
            DropIndex("dbo.Cwes", new[] { "CweId" });
            DropIndex("dbo.PackageDeprecationCves", new[] { "PackageDeprecation_Key" });
            DropIndex("dbo.PackageDeprecationCves", new[] { "Cve_Key" });
            DropIndex("dbo.PackageDeprecationCwes", new[] { "PackageDeprecation_Key" });
            DropIndex("dbo.PackageDeprecationCwes", new[] { "Cwe_Key" });
            Sql("ALTER TABLE [dbo].[PackageDeprecations] DROP CONSTRAINT IF EXISTS [dbo.PackageDeprecations_CVSSRating];");
            DropColumn("dbo.PackageDeprecations", "CvssRating");
            DropTable("dbo.Cves");
            DropTable("dbo.Cwes");
            DropTable("dbo.PackageDeprecationCves");
            DropTable("dbo.PackageDeprecationCwes");
        }
        
        public override void Down()
        {
            CreateTable(
                "dbo.PackageDeprecationCwes",
                c => new
                    {
                        PackageDeprecation_Key = c.Int(nullable: false),
                        Cwe_Key = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.PackageDeprecation_Key, t.Cwe_Key });
            
            CreateTable(
                "dbo.PackageDeprecationCves",
                c => new
                    {
                        PackageDeprecation_Key = c.Int(nullable: false),
                        Cve_Key = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.PackageDeprecation_Key, t.Cve_Key });
            
            CreateTable(
                "dbo.Cwes",
                c => new
                    {
                        Key = c.Int(nullable: false, identity: true),
                        CweId = c.String(nullable: false, maxLength: 20, unicode: false),
                        Name = c.String(nullable: false, maxLength: 200),
                        Description = c.String(nullable: false, maxLength: 300),
                        Listed = c.Boolean(nullable: false),
                        Status = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Key);
            
            CreateTable(
                "dbo.Cves",
                c => new
                    {
                        Key = c.Int(nullable: false, identity: true),
                        CveId = c.String(nullable: false, maxLength: 20, unicode: false),
                        Description = c.String(maxLength: 300),
                        LastModifiedDate = c.DateTime(),
                        PublishedDate = c.DateTime(),
                        Listed = c.Boolean(nullable: false),
                        Status = c.Int(nullable: false),
                        CvssRating = c.Decimal(precision: 3, scale: 1),
                    })
                .PrimaryKey(t => t.Key);
            
            AddColumn("dbo.PackageDeprecations", "CvssRating", c => c.Decimal(precision: 3, scale: 1));
            Sql("ALTER TABLE [dbo].[PackageDeprecations] ADD CONSTRAINT [dbo.PackageDeprecations_CVSSRating] CHECK ([CVSSRating] >= 0 AND [CVSSRating] <= 10)");
            CreateIndex("dbo.PackageDeprecationCwes", "Cwe_Key");
            CreateIndex("dbo.PackageDeprecationCwes", "PackageDeprecation_Key");
            CreateIndex("dbo.PackageDeprecationCves", "Cve_Key");
            CreateIndex("dbo.PackageDeprecationCves", "PackageDeprecation_Key");
            CreateIndex("dbo.Cwes", "CweId", unique: true);
            CreateIndex("dbo.Cves", "CveId", unique: true);
            AddForeignKey("dbo.PackageDeprecationCwes", "Cwe_Key", "dbo.Cwes", "Key", cascadeDelete: true);
            AddForeignKey("dbo.PackageDeprecationCwes", "PackageDeprecation_Key", "dbo.PackageDeprecations", "Key", cascadeDelete: true);
            AddForeignKey("dbo.PackageDeprecationCves", "Cve_Key", "dbo.Cves", "Key", cascadeDelete: true);
            AddForeignKey("dbo.PackageDeprecationCves", "PackageDeprecation_Key", "dbo.PackageDeprecations", "Key", cascadeDelete: true);
        }
    }
}
